// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    animationData: {},
    animationRotateData: {},
    timer: null,
    status: 'play',
    musicIconStatus: 'pause',

    innerAudioContext: null
  },
  onLoad() {
    // var animationRotateData = wx.createAnimation({
    //   duration: 1000,
    //   timingFunction: 'linear'
    // })
    // let idx = 0
    // let timer = setInterval(() => {
    //   // animationRotateData.rotate(360 + 360 * idx).step()
    //   idx = idx + 1
    //   animationRotateData.rotate(180 * idx).step()
    //   this.setData({
    //     animationRotateData: animationRotateData.export()
    //   })
    // }, 1000)

    // this.setData({
    //   timer
    // })
    const innerAudioContext = wx.createInnerAudioContext({
      useWebAudioImplement: false
    })
    innerAudioContext.src = 'http://music.163.com/song/media/outer/url?id=317151.mp3'

    this.setData({
      innerAudioContext,
    })
    this.playActivi()
  },
  onShow() {

  },
  onReady() {

  },
  onHide() {
    
  },

  onUnload() {
    const timer = this.data.timer
    clearInterval(timer)

    var animationRotateData =  wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear'
    })
    animationRotateData.rotate(0).step()

    this.setData({
      timer: null,
      animationRotateData: animationRotateData.export()
    })
  },

  changeStatus() {
    const status = this.data.status === 'play' ? 'pause' : 'play'
    this.setData({
      status
    })
    if(status === 'pause') {
      this.pauseActivi()
      this.setData({
        musicIconStatus: 'pause'
      })
      const innerAudioContext = this.data.innerAudioContext
      innerAudioContext.pause()
    } else {
      this.playActivi()
    }
  },

  playActivi() {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
      transformOrigin: '0% 0% 0'
    })

    animation.rotate(20).step()

    this.setData({
      animationData: animation.export()
    })

    setTimeout(() => {
      this.setData({
        musicIconStatus: 'play'
      })
      // this.data.innerAudioContext.play()
      const innerAudioContext = this.data.innerAudioContext
      innerAudioContext.play()
    }, 1000)

    var animationRotateData = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear'
    })
    
    let idx = 0
    let timer = setInterval(() => {
      // animationRotateData.rotate(360 + 360 * idx).step()
      idx = idx + 1
      animationRotateData.rotate(180 * idx).step()
      this.setData({
        animationRotateData: animationRotateData.export()
      })
    }, 1000)

    this.setData({
      timer
    })
  },

  pauseActivi() {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
      transformOrigin: '0% 0% 0'
    })
    animation.rotate(0).step()

    const timer = this.data.timer
    clearInterval(timer)

    this.setData({
      animationData: animation.export(),
      timer: null
    })
  },
  
  callPhone(e) {
    const number = e.currentTarget.dataset.number
    wx.makePhoneCall({
      phoneNumber: number
    })
  }
})
